package com.zn.service.impl;

import com.zn.entity.AppCustomer;
import com.zn.entity.CustomerMessageRecord;
import com.zn.mapper.AppCustomerMapper;
import com.zn.mapper.CustomerMessageRecordMapper;
import com.zn.service.AppCustomerService;
import com.zn.web.dto.AppCustomerDto;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by yinjiawei on 2018/8/2.
 */
@Service
public class AppCustomerServiceImpl implements AppCustomerService{
    @Autowired
    private AppCustomerMapper appCustomerMapper;
    @Autowired
    private CustomerMessageRecordMapper customerMessageRecordMapper;

    @Override
    public AppCustomer selectByPrimaryKey(String customerId) {
        return appCustomerMapper.selectByPrimaryKey(customerId);
    }

    @Override
    public List<CustomerMessageRecord> findCustomerMessageRecordAll(AppCustomerDto record) {
        List<CustomerMessageRecord> list = customerMessageRecordMapper.findAll(record);
        for (int i = 0; i < list.size(); i++){//循环截取，获取最后最新的数据展示
            if (StringUtils.isNotBlank(list.get(i).getAnswerContent())) {
                String[] str = list.get(i).getAnswerContent().split("\\|");
                list.get(i).setAnswerContent(str[str.length-1]);
            }
        }
        return list;
    }

    @Override
    public int findCustomerMessageRecordCount(CustomerMessageRecord record) {
        return customerMessageRecordMapper.findCount(record);
    }

    @Override
    public int saveCustomerMessageRecord(CustomerMessageRecord record) {
        if (!"".equals(record.getCustomerMessageId()) && record.getCustomerMessageId() != null){
//            CustomerMessageRecord customerMessageRecord = customerMessageRecordMapper.selectByPrimaryKey(record.getCustomerMessageId());
//            if ("0".equals(customerMessageRecord.getMessageStatus())){
//                record.setMessageStatus("1");
//            }else {//多次编辑回复，追加并记录每次回复信息和操作人信息
//                record.setAnswerContent(customerMessageRecord.getAnswerContent()+customerMessageRecord.getUpdateUser()+customerMessageRecord.getUpdateTime()+"|"+record.getAnswerContent());
//            }
            return customerMessageRecordMapper.updateByPrimaryKeySelective(record);
        }else {
            AppCustomer appCustomer = selectByPrimaryKey(record.getCustomerId());
            if (StringUtils.isBlank(record.getCustomerId()) || StringUtils.isBlank(record.getMessageContent())
                    || appCustomer == null){
                return -1;
            }
            record.setBusinessId(appCustomer.getBusinessId());
            record.setMessageStatus("0");
            record.setCreateTime(new Date());
            return customerMessageRecordMapper.insertSelective(record);
        }
    }

    @Override
    public int updateSsqContractId(AppCustomer appCustomer) {
        return appCustomerMapper.updateSsqContractId(appCustomer);
    }
}
